<template>

		<div class="row">
			<div class="col-md-8 col-md-offset-2">
				<div class="panel panel-default">
					<div class="panel-heading">文章首页</div>

					<div v-for="post in posts" :key="post.id"class="bs-callout bs-callout-danger">
						<h4><router-link :to="{name:'posts',params: {id:post.id}}">{{ post.title }}</router-link></h4>
						<p>{{ post.body }}</p>
					</div>
				</div>
				<span @click="ni" class="hand">{{name1}}</span>
				<span @click="xia" class="hand">{{name2}}</span>
			</div>
		</div>

</template>

<script>
    export default {
        mounted(){
            axios.get('/api/posts').then(response =>{
                this.posts = response.data.data;
                this.mixnuber = response.data.last_page;
            }).catch(error=>{
                console.log(error);
            });

        },
        data(){
            return {
                posts:[],
                number:1,
                mixnuber:null,
                name1:"下一页",
                name2:"第一页"
            }
        },
        methods:{

            ni:function() {

                this.number+=1;
                this.name2 = '上一页';
                if(this.number <= this.mixnuber){
                    axios.get('/api/posts?page='+this.number).then(response =>{
                        this.posts = response.data.data;
                        console.log(this.posts);
                        if(this.number === this.mixnuber){
                            this.name1 = "最后一页";

                        };
                    }).catch(error=>{
                        console.log(error);
                    });
                }else {
                    this.name1 = "最后一页";
                }

            },
            xia:function () {
                if(this.number > 1 ){
                    this.number-=1;
                    this.name1="下一页";
                    axios.get('/api/posts?page='+this.number).then(response =>{
                        this.posts = response.data.data;
                        console.log(this.posts);
                        if(this.number === 1){
                            this.name2 = '第一页'
                        }

                    }).catch(error=>{
                        console.log(error);
                    });
                }else {
                    this.name2 = '第一页';
                }
            }
        }

    }
</script>

